<?php

/**
 * This is the model class for table "{{user}}".
 *
 * The followings are the available columns in table '{{user}}':
 * @property integer $id
 * @property string $username
 * @property string $fullname
 * @property string $password
 * @property string $email
 * @property string $mobile
 * @property string $createddate
 * @property integer $role_id
 * @property integer $isactive
 * @property string $activekey
 *
 * The followings are the available model relations:
 * @property Role $roles
 * @property Comment[] $comments
 * @property Profession[] $professions
 * @property Proposal[] $proposals
 * @property Rate[] $rates
 */
class User extends CActiveRecord
{
	public $password_repeat;
	public $verifyCode;
	public $newPassword;
	public $confirmPassword;
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{user}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('role_id,isactive', 'numerical', 'integerOnly'=>true),
			array('username, password, email,isactive', 'length', 'max'=>128),
			array('fullname','length','max'=>256),
			array('password_repeat', 'length', 'max'=>128,'on'=>'create,register'),
			array('username,fullname, password, email', 'required','message'=>Yii::t('default','required.{attribute}'),'on'=>'create,register,update,updateProfile'),
			array('password_repeat', 'required','message'=>Yii::t('default','required.{attribute}'),'on'=>'create,register'),
			array('password', 'compare','message'=>Yii::t('default','exact.{attribute}'),'on'=>'create,register'),
			array('password_repeat', 'safe'),
			array('mobile', 'safe'),
			array('username,email', 'unique','message'=>Yii::t('default','unique.{attribute}')),
			array('email','email','message'=>Yii::t('default','email.{attribute}')),	
			array('mobile','numerical','message'=>Yii::t('default','integer.{attribute}')),
			array('password,newPassword,confirmPassword', 'required','on'=>'changePassword','message'=>Yii::t('default','required.{attribute}')),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, username,fullname, password, email, role_id,isactive,activekey,mobile, createddate,', 'safe', 'on'=>'search'),
			//array('id, username, email, role_id,isactive,activekey', 'safe', 'on'=>'changePassword'),
			array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements(),'captchaAction' => 'site/captcha','on'=>'register'),
			array('verifyCode','safe'),	
			array('username', 'required','on'=>'social'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'comments' => array(self::HAS_MANY, 'Comment', 'userid'),
			'professions' => array(self::HAS_MANY, 'Profession', 'created_by'),
			'proposals' => array(self::HAS_MANY, 'Proposal', 'userid'),
			'rates' => array(self::HAS_MANY, 'Rate', 'userid'),
			'roles' => array(self::BELONGS_TO, 'Role', 'role_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'username' =>  Yii::t('user_messages','Username'),
			'password' => Yii::t('user_messages','Password'),
			'email' => Yii::t('user_messages','E-mail'),
			'password_repeat' => Yii::t('user_messages','Confirm-Password'),
			'verifyCode' => Yii::t('user_messages','Verify Code'),
			'fullname' => Yii::t('user_messages','Fullname'),
			'mobile' => Yii::t('user_messages','Mobile'),
			'role_id' => Yii::t('user_messages','Role'),
			'isactive' => Yii::t('user_messages','Status'),
			'confirmPassword'=>	Yii::t('user_messages','confirmPassword'),
			'newPassword'=>Yii::t('user_messages','newPassword'),
				
		);
	}
	
	
	public function getRoleString(){
		return Yii::t('default','role_'.$this->role_id);
	}
	
	public function getStatusString(){
		return Yii::t('default','status_'.$this->isactive);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('fullname',$this->fullname,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('mobile',$this->mobile,true);
		$criteria->compare('createddate',$this->createddate,true);
		$criteria->compare('role_id',$this->role_id);
		$criteria->compare('isactive',$this->isactive);
		$criteria->compare('activekey',$this->activekey,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
